﻿<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE xsl:stylesheet  [
    <!ENTITY nbsp   "&#160;">
    <!ENTITY copy   "&#169;">
    <!ENTITY reg    "&#174;">
    <!ENTITY trade  "&#8482;">
    <!ENTITY mdash  "&#8212;">
    <!ENTITY ldquo  "&#8220;">
    <!ENTITY rdquo  "&#8221;">
    <!ENTITY pound  "&#163;">
    <!ENTITY yen    "&#165;">
    <!ENTITY euro   "&#8364;">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:import href="mainframe.xsl"/>
    <xsl:param name="appindex"/>
    
    <xsl:template name="main">
        <xsl:variable name="app" select="/root/system/url/@app" />
        
        <xsl:if test="/root/system/url/@action='buildindex'">
            <h1>索引列表</h1>
            选择数据库：<select onchange="changeDb(this.value)">
                <xsl:for-each select="$appindex/root/app">
                    <option value="{id}">
                        <xsl:if test="id=$app">
                            <xsl:attribute name="selected">selected</xsl:attribute>
                        </xsl:if>
                        <xsl:value-of select="id"/>(<xsl:value-of select="name"/>)
                    </option>
                </xsl:for-each>
            </select>
            <br/>
            <br/>
            <a href="?app={$app}&amp;action=buildindexedit">创建索引</a>
            <table border="1" cellpadding="4" cellspacing="0">
                <tr>
                    <th>索引名称</th>
                    <th>从属表名</th>
                    <th>包含字段</th>
                    <th>操作</th>
                </tr>
                <xsl:for-each select="/root/item">
                    <tr>
                        <td>
                            <xsl:value-of select="@name"/>
                        </td>
                        <td>
                            <xsl:value-of select="@tbl_name"/>
                        </td>
                        <td>
                            <xsl:value-of select="@sql"/>
                        </td>
                        <td>
                            <a href="?app={$app}&amp;index={@name}&amp;action=vacuum" title="VACUUM">整理</a>&nbsp;
                            <a href="?app={$app}&amp;index={@name}&amp;action=reindex" title="REINDEX">重建</a>&nbsp;
                            <a href="?app={$app}&amp;table={@tbl_name}&amp;index={@name}&amp;action=buildindexedit">修改</a>&nbsp;
                            <a href="javascript:void(0)" onclick="removeIndex(this,'{$app}','{@name}')">删除</a>
                        </td>
                    </tr>
                </xsl:for-each>
            </table>
        </xsl:if>

        <xsl:if test="/root/system/url/@action='buildindexedit'">
            <h1>
                <xsl:choose>
                    <xsl:when test="not(/root/system/url/@table) or not(/root/system/url/@index)">
                        创建索引
                    </xsl:when>
                    <xsl:otherwise>
                        修改索引
                    </xsl:otherwise>
                </xsl:choose>
            </h1>
            <form method="post" name="form1">
                <a href="?app={$app}&amp;action=buildindex">
                    索引列表
                </a>
                <table border="1" cellpadding="4" cellspacing="0" class="buildindex">
                    <tr>
                        <th>索引名称</th>
                        <td>
                            <input type="text" name="indexname" value="{/root/system/url/@index}" />
                            <input type="hidden" name="indexoldname" value="{/root/system/url/@index}" />
                        </td>
                    </tr>
                    <tr>
                        <th>从属表名</th>
                        <td>
                            <xsl:choose>
                                <xsl:when test="/root/system/url/@table">
                                    <input type="hidden" name="indextable" id="tables" value="{/root/system/url/@table}" />
                                    <xsl:value-of select="/root/system/url/@table"/>
                                </xsl:when>
                                <xsl:otherwise>
                                    <select name="indextable" id="tables" onchange="GetColsForIndex()">
                                        <xsl:for-each select="/root/app/entities/entity">
                                            <option title="{@en-us}" value="Entity-{@name}-Data">
                                                Entity-<xsl:value-of select="@name"/>-Data(<xsl:value-of select="@zh-cn"/>)
                                            </option>
                                            <option title="{@en-us}" value="Entity-{@name}-Category">
                                                Entity-<xsl:value-of select="@name"/>-Category(<xsl:value-of select="@zh-cn"/>)
                                            </option>
                                        </xsl:for-each>
                                    </select>
                                </xsl:otherwise>
                            </xsl:choose>

                        </td>
                    </tr>
                    <tr>
                        <th>选择字段</th>
                        <td>
                            <div id="colsindexbox">
                                
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <th>&nbsp;</th>
                        <td>
                            <input type="submit" value="保存" />
                        </td>
                    </tr>
                </table>
            </form>
            <script type="text/javascript">
                function GetColsForIndex(){
                var app = "<xsl:value-of select="/root/system/url/@app"/>";
                var index = "<xsl:value-of select="/root/system/url/@index"/>";
                var arr = $("#tables").val().split("-");
                var entity = arr[1];
                var iscat = arr[2]=="Data"?0:1;
                $.get("?app="+app+"&amp;entity="+entity+"&amp;index="+index+"&amp;iscat="+iscat+"&amp;action=getcolsforindex", function(x){
                $("#colsindexbox").html(x);
                });
                }
                GetColsForIndex();
            </script>
        </xsl:if>
        
    </xsl:template>

    <xsl:template name="css">
        <style type="text/css">
            #colsindexbox {width:610px;}
            .buildindex label { float:left; width:200px; margin:2px 0; height:20px; overflow:hidden}
            .buildindex label span {color:gray; margin-left:2px}
        </style>
    </xsl:template>

    <xsl:template name="js">
        <script type="text/javascript">
            <xsl:comment>
                function changeDb(app)
                {
                    setTimeout(function(){{location.href='?app='+app+'&amp;action=buildindex'}},10)
                }
                function removeIndex(obj, app, indexname){
                    if(!confirm("确定要删除索引 "+ indexname + " 吗？"))
                    {
                        return;
                    }
                    location.href="?app="+app+"&amp;index="+indexname+"&amp;action=removeindex";
                }
            </xsl:comment>
        </script>
    </xsl:template>
    
</xsl:stylesheet>